package cn.git.agent.init;

import cn.git.agent.basic.BasicTransformInfo;
import cn.git.agent.basic.ControllerTransformInfo;
import jdk.nashorn.internal.ir.debug.JSONWriter;
import jdk.nashorn.internal.parser.JSONParser;

import java.lang.reflect.InvocationTargetException;

/**
 * @description:
 * @program: bank-credit-sy
 * @author: lixuchun
 * @create: 2024-03-18 03:06:14
 */
public abstract class AbstractCollect {

    /**
     * @description: 修改开始,设置通用自定义参数信息
     * @param className
     * @return
     */
    public BasicTransformInfo modifyBegin(String className, String methodName) {
        BasicTransformInfo basicTransformInfo = new BasicTransformInfo();
        basicTransformInfo.setBegin(System.currentTimeMillis());
        basicTransformInfo.setCreateTime(System.currentTimeMillis());
        return basicTransformInfo;
    }

    /**
     * @description: 修改结束,设置通用自定义参数信息
     * @param basicTransformInfo
     * @return
     */
    public void modifyEnd(BasicTransformInfo basicTransformInfo) {
        basicTransformInfo.setEnd(System.currentTimeMillis());
        Long useTime = basicTransformInfo.getEnd() - basicTransformInfo.getBegin();
        basicTransformInfo.setUserTime(useTime);
        // todo:通过http发送消息到server端
        System.out.println("todo : 发送采集消息到服务端-> : ".concat(basicTransformInfo.toString()));
    }

    /**
     * @description: 修改异常,设置通用自定义参数信息
     * @param basicTransformInfo
     * @param throwable
     * @return
     */
    public void modifyError(BasicTransformInfo basicTransformInfo, Throwable throwable) {
        if (basicTransformInfo != null) {
            if (throwable instanceof InvocationTargetException) {
                basicTransformInfo.setErrorType(((InvocationTargetException) throwable).getTargetException().getClass().getName());
                basicTransformInfo.setErrorMsg(((InvocationTargetException) throwable).getTargetException().getMessage());
            } else {
                basicTransformInfo.setErrorMsg(throwable.getMessage());
                basicTransformInfo.setErrorType(throwable.getClass().getName());
            }
        }
        // todo:通过http发送消息到server端
        System.out.println("todo : 发送采集消息到服务端-> : ".concat(basicTransformInfo.toString()));
    }

}
